<?php

class Zend_View_Helper_NavPagina extends Zend_View_Helper_Abstract {
	
	public function navPagina() {
		$menus = $this->view->nav_pagina;
		if (!$menus)
			$menus = AbstractDbTable::get('pagina')->getMenu('navegacao');
		
		return $this->mountNavPagina($menus);
	}
	
	public function mountNavPagina($menus, $id = 0) {
		$request = Zend_Controller_Front::getInstance()->getRequest();
		$html = '';
		$url = $this->view->baseUrl . '/pagina/';
		$controller = $request->getControllerName();
		$chave = $request->getParam('chave', false);
			
		$i = 0;
		while ($i < count($menus)) {
			$menu = $menus[$i];
			
			if ($menu['pagina_id'] == $id) {
				$submenu = $this->mountNavPagina($menus, $menu['id']);
				
				$html .= '<li';
				
				if ($submenu)
					$html .= ' class="com-sub"';

				if ($menu['chave'] == 'grupo-lwart')
					$html .= '><a rel="external" href="';
				else
					$html .= '><a href="';
				
//				if (!$submenu)
				if ($menu['chave'] == 'grupo-lwart')
					$html .= 'http://www.lwart.com.br';
				else
					$html .= $url . $menu['chave'];
//				else
//					$html .= '#';
				
				if ($menu['chave'] == $chave || $menu['chave'] == $controller)
					$html .= '" class="active';
				
				$html .= '">&raquo; ' . $menu['nome'] . '</a>';
				
				if ($submenu) {
					$html .= '<ul class="submenu">';
					$html .= $submenu;
					$html .= '</ul>';
				}
				
				$html .= '</li>';
			}
			
			++$i;
		}
		
		return $html;
	}
}